﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSVExportImport.Columns.Templates;
using CSVExportImport;
using System.IO;
using CSVExportImport.Columns;
using ProjectStorageDB;
using ProjectStorageLib.Project;
using ProjectStorageLib.ParameterizedNomencaltures;
using ProjectStorageDB.Objects;

namespace StorageProject.DocumentGenerator.CSV
{
    public class CSSTemplate
    {
        Processor processor = new Processor();

        public CSSTemplate()
        {
            addColumns();
        }

        public String Export(List<ProjectStorageLib.Project.ProjectManager> projectList)
        {
            StringWriter w = new StringWriter();
            processor.Export(w, projectList);
            return w.ToString();
        }

        public ProjectsImporterManager Import(TextReader reader, decimal userid)
        { 
            ProjectsImporterManager a = new ProjectsImporterManager();
            processor.Import(reader, () => { 
                var p = a.GetNewManager(); 
                p.Project.UserID = userid; 
                return p; 
            });
            return a;
        }

        protected void addColumns()
        {
            processor.AddColumn(new StringColumnTemplate("Name", "Name"));
            processor.AddColumn(new StringColumnTemplate("Description", "Description"));

            AllNomenclatures a = (new NomenclatureRetriever()).GetAll();
            a.Factors.ForEach(f => processor.AddColumn(new FactorColumn(f)));
            a.Practices.ForEach(p => processor.AddColumn(new PracticeColumn(p)));

        }

        public class FactorColumn : IColumnTemplate
        {
            protected Factor factor;
            public FactorColumn(Factor factor)
            {
                this.factor = factor;
            }

            public string Header
            {
                get
                {
                    return factor.Name;
                }
                set
                {

                }
            }

            public string GetValue(object obj)
            {
                var o = (ProjectManager)obj;
                var s = o.GetScaleValueByFactor(factor);
                if (s == null) return null;
                return s.FactorScale.Value;
            }

            public void SetValue(object obj, string value)
            {
                var o = (ProjectManager)obj;
                if (String.IsNullOrEmpty(value))
                {
                    o.DeleteFactor(factor.FactorID);
                }
                else
                {
                    var sv = factor.FactorScales.Where(s => s.Value == value).First().FactorScaleID;
                    o.SetScaleValueByFactor(factor.FactorID, sv);
                }
            }
        }
        public class PracticeColumn : IColumnTemplate
        {
            protected AgilePractice practice;
            public PracticeColumn(AgilePractice practice)
            {
                this.practice = practice;
            }

            public string Header
            {
                get
                {
                    return practice.Name;
                }
                set
                {

                }
            }

            public string GetValue(object obj)
            {
                var o = (ProjectManager)obj;
                var s = o.GetPracticeValueByPractice(practice);
                if (s == null) return null;
                return s.WrappedValue.ToString();
            }

            public void SetValue(object obj, string value)
            {
                var o = (ProjectManager)obj;
                if (String.IsNullOrEmpty(value))
                {
                    o.DeletePractice(practice.PracticeID);
                }
                else
                {
                    var v = (EnumPracticeUsingValue)Enum.Parse(typeof(EnumPracticeUsingValue), value);
                    o.SetPracticeValueByFactor(practice.PracticeID, v);
                }
            }
        }

    }
}
